$("#logisticsList").click(function () {
    var url = "/page/admin/logistList";
    $("#body").load(url);
    setTimeout(function () {
        addDate_logistList(8, 1);
    }, 300);
});


$("#studentList").click(function () {

    var url = "/page/admin/studentList";
    $("#body").load(url);
    setTimeout(function () {
        addData_studentList(8, 1);
    }, 300);

});

$("#collegeList").click(function () {
    var url = "/page/admin/collegeList";
    $("#body").load(url);

});
$("#home").click(function () {
    var url = "/page/admin/home";
    $("#body").load(url);
})

$("#dormList").click(function () {

    var url = "/page/admin/dormList";
    var loadDiv;
    $("#body").load(url);
    localStorage.removeItem("tungNum");
    localStorage.removeItem("roomNum");
    setTimeout(function () {
        loadDiv = load(".list", 99999);
    }, 50);
    setTimeout(function () {
        addDormList(1, 7, "", "", function () {
            tableChange("#dormListTable");
        });
        load_close(loadDiv);
    }, 500);

});

var addDate_logistList = function (pageSize, pageNumber) {
    var loadDiv = load($(".list"), 999699);
    $(".list-content").children("table").children("tbody").html("");
    $(".chooseNum").children("select").html("");
    if (isNull(pageSize)) {
        pageSize = 8;
    }
    if (isNull(pageNumber)) {
        pageNumber = 1;
    }
    $.ajax({
        url: "repair/list",
        type: "post",
        data: {
            "token": localStorage['token'],
            "pageSize": pageSize,
            "pageNumber": pageNumber,
        },
        success: function (data) {
            console.log(data);
            if (data.code === 200) {
                listSetting(data.data.totalPages, data.data.pageNumber);
                localStorage['pageSize'] = data.data.pageSize + "";
                localStorage['pageNumber'] = data.data.pageNumber + "";
                var length = data.data.content.length;
                for (var j = 0; j < data.data.totalPages; j++) {
                    var option = addRealOption(j + 1);
                    if ((j + 1) == data.data.pageNumber) {
                        option.attr("selected", "selected");
                    }
                    option.appendTo($(".chooseNum").children("select"));
                }
                for (var i = 0; i < length; i++) {

                    var user = data.data.content[i];
                    var tr = $('<tr></tr>');
                    addTd(user.id).appendTo(tr);
                    addTd(user.name).appendTo(tr);
                    addTd(user.gender).appendTo(tr);
                    addTd(user.phone).appendTo(tr);
                    $(' <td>\n' + ' <button  onclick="repair_detail($(this))" >查看</button>\n' + ' </td>').appendTo(tr);
                    tr.appendTo($(".list-content").children("table").children("tbody"))
                }
                $(".thisNum").children("span").text(data.data.pageNumber);
                // console.log(new Date().getSeconds() );
                if ($(".list-content").children("table").children("tbody").text() === "") {
                    var bk = getNothingBackground();
                    bk.appendTo($(".list-content").children("table").children("tbody"));
                }

            } else {
                // console.log(datas.msg);
                errorMessage(data.msg);
            }
            load_close(loadDiv);
        }
    })
};

var repair_detail = function (btn) {
    var loadDiv = load("#body", 9999999);
    var bodyurl = "/page/admin/repairpersonaldetail";
    $("#body").load(bodyurl);
    var url = "repair/detail";
    var id = btn.parent().parent().children(":first").text();
    $("#taskListTable").children("tbody").text();
    $.ajax({
        url: url,
        type: "post",
        data: {
            "token": localStorage['token'],
            "id": id
        },
        success: function (data) {
            console.log(data);
            if (data.code == 200) {
                var task = data.data.task;
                if (task != null && task.length > 0) {
                    for (var i = 0; i < task.length; i++) {
                        var tr = $("<tr></tr>");
                        addTd(task[i].dorm).appendTo(tr);
                        addTd(task[i].device).appendTo(tr);
                        addTd(task[i].content).appendTo(tr);
                        var state = stateChangeText(task[i].state);
                        addTd(state).appendTo(tr);
                        // $("<td><button onclick=''>已处理</button></td>").appendTo(tr);
                        addTdHidId(task.dorm).appendTo(tr);
                        tr.appendTo($("#taskListTable").children("tbody"));
                    }
                }
                tableChange("#taskListTable");
                $("#name").val(data.data.name);
                $("#phone").val(data.data.phone);
                $("#taskNum").val(data.data.taskNum);
                localStorage['personalId'] = data.data.id;
            } else {
                errorMessage(data.msg);
            }
            load_close(loadDiv);
        }
    })
};

var tableChange = function (tableSelector) {
    var ths = $(tableSelector).children("thead").children("tr").children("th");

    var tds = $(tableSelector).children("tbody").children("tr").first().children("td");
    var td = tds.first();
    for (var i = 0; i < ths.length; i++) {
        var thWidht = ths[i].getBoundingClientRect().width;
        td.innerWidth(thWidht);
        td = tds.next();
    }

};

$("#analysis").click(function () {
    var url = "/page/admin/analysis";
    $("#body").load(url);
})

$("#repiarList").click(function () {
    var url = "/page/admin/repairList";
    $("#body").load(url);
});
var addData_studentList = function (pageSize, pageNumber, keyword, gender, clas, college) {
    var loadDiv = load($(".list"), 99999);
    $(".list-content").children("table").children("tbody").html("");
    $(".chooseNum").children("select").html("");
    if (isNull(pageSize)) {
        pageSize = 8;
    }
    if (isNull(pageNumber)) {
        pageNumber
    }
    // console.log(clas);
    keyword = isnullTONull(keyword);
    gender = isnullTONull(gender);
    clas = isnullTONull(clas);
    college = isnullTONull(college);
    console.log(clas);
    $.ajax(
        {
            url: "student/list",
            type: "post",
            data: {
                "token": localStorage['token'],
                "pageSize": pageSize,
                "keyword": keyword,
                "pageNumber": pageNumber,
                "gender": gender,
                "clas": clas,
                "college": college
            },
            success: function (datas) {
                if (datas.code == 200) {
                    // console.log(datas);
                    listSetting(datas.data.totalPages, datas.data.pageNumber);
                    localStorage['pageSize'] = datas.data.pageSize + "";
                    localStorage['pageNumber'] = datas.data.pageNumber + "";
                    var length = datas.data.content.length;
                    for (var j = 0; j < datas.data.totalPages; j++) {
                        var option = addRealOption(j + 1);
                        if ((j + 1) == datas.data.pageNumber) {
                            option.attr("selected", "selected");
                        }
                        option.appendTo($(".chooseNum").children("select"));
                    }

                    for (var i = 0; i < length; i++) {

                        var user = datas.data.content[i];
                        var tr = $('<tr></tr>');
                        addTd(user.id).appendTo(tr);
                        addTd(user.name).appendTo(tr);
                        addTd(user.college).appendTo(tr);
                        var clasTd = addTd("");
                        clasTd.text(user.grade + user.collegeId + user.clas);
                        clasTd.appendTo(tr);
                        addTd(user.gender).appendTo(tr);
                        var dormTd = addTd("");
                        dormTd.text(user.tungNum + "-" + user.roomNum);
                        dormTd.appendTo(tr);

                        $(' <td>\n' + ' <button  onclick="detailBtn($(this))" >查看</button>\n' + ' </td>').appendTo(tr);
                        tr.appendTo($(".list-content").children("table").children("tbody"))

                    }
                    $(".thisNum").children("span").text(datas.data.pageNumber);
                    // console.log(new Date().getSeconds() );
                    if ($(".list-content").children("table").children("tbody").text() === "") {
                        var bk = getNothingBackground();
                        bk.appendTo($(".list-content").children("table").children("tbody"));
                    }
                } else {
                    // console.log(datas.msg);
                    errorMessage(datas.msg);
                }
                load_close(loadDiv);
            }

        }
    );
    // setTimeout(function () {
    //
    // }, 1000);
};


var stateChangeText = function (code) {
    var msg = "";
    if (code == 0) {
        msg = "审核中";
    } else if (code == 1) {
        msg = "分配任务中";
    } else if (code == 2) {
        msg = "处理中";
    } else if (code == 3) {
        msg = "已处理";
    } else if (code == 4) {
        msg = "未通过审核";
    }
    return msg;
};